package com.asjy.controller;

import com.asjy.po.User;
import com.oracle.xmlns.internal.webservices.jaxws_databinding.SoapBindingUse;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @Auther: lilinlin
 * @Date: 2021/4/20
 * @Description: com.asjy.controller
 * @version: 1.0
 */

@RestController //@Controller+@ResponseBody  作用：1。生成bean 2.返回的全是字符串，不走视图解析器
//注意：必须写在类中，不能写再方法上，作用：当前类型的说有方法相当于都加上了@ResponseBody注解
public class RestfulController {
    //http://localhost:8080/shop/rest1/1/2
    @RequestMapping("/rest1/{a}/{b}")
    public String rest1(@PathVariable(value = "a") Integer aa, @PathVariable(value="b") Integer bb, Model model){
        Integer res = aa+bb;
        model.addAttribute("msg",res);
        return "rest";
    }
    //post:新增 get：查询  put：修改 delete：删除
    //@RequestMapping(value="/user",method = RequestMethod.POST)
    @PostMapping("/user")
    public String addUser(User user,Model model){
        System.out.println(user);
        System.out.println("添加用户");
        model.addAttribute("msg","添加成功");
        return "post";
    }

    //@RequestMapping(value="/user",method = RequestMethod.PUT)
    @PutMapping("/user")//api接口
    public String updateUser(User user,Model model){
        System.out.println(user);
        System.out.println("修改用户");
        model.addAttribute("msg","修改成功");
        return "put";
    }

    //@RequestMapping(value="/user/{id}",method = RequestMethod.GET)
    @GetMapping("/user/{id}")
    public String findUser(@PathVariable Integer id, Model model){
        System.out.println("查询用户");
        model.addAttribute("msg","查询成功"+id);
        return "get";
    }

    //@RequestMapping(value="/user/{id}",method = RequestMethod.DELETE)
    @DeleteMapping("/user/{id}")
    public String deleteUser(@PathVariable Integer id, Model model){
        System.out.println("删除用户");
        model.addAttribute("msg","删除成功"+id);
        return "delete";
    }

}